class Solution {
    public int countCharacters(String[] words, String chars) {
        Map<Character,Integer> mapS = new HashMap<>();
        int countS = 0;
        for(char ch : chars.toCharArray()){
            countS = mapS.getOrDefault(ch,0) + 1;
            mapS.put(ch,countS);
        }
        int countC = 0;
        int sum = 0;
        for(int i = 0; i < words.length; i++){
            Map<Character,Integer> mapC = new HashMap<>();
            String str = words[i];
            for(char ch: str.toCharArray()){
                countC = mapC.getOrDefault(ch,0) + 1;
                mapC.put(ch,countC);
            }
            boolean flg = true;
            for(int j = 0; j < str.length(); j++){
                if(mapC.get(str.charAt(j))> mapS.getOrDefault(str.charAt(j),0)){
                   flg = false; 
                   break;
                }
            }
            if(flg) sum += str.length();
        }
        return sum;
    }
}
